/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.settlement.service.carinvoice;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.settlement.dao.carinvoice.CarInvoiceDao;
import com.jeesite.modules.settlement.entity.carinvoice.CarInvoice;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContract;
import com.jeesite.modules.settlement.entity.customercarcontract.CustomerCarContractDetail;
import com.jeesite.modules.settlement.service.customercarcontract.CustomerCarContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 车辆发票Service
 * @author LV
 * @version 2021-03-09
 */
@Service
@Transactional(readOnly=true)
public class CarInvoiceService extends CrudService<CarInvoiceDao, CarInvoice> {

    @Autowired
    private CustomerCarContractService customerCarContractService;
    @Autowired
    private CodeRuleService codeRuleService;


	/**
	 * 获取单条数据
	 * @param carInvoice
	 * @return
	 */
	@Override
	public CarInvoice get(CarInvoice carInvoice) {
		return super.get(carInvoice);
	}
	
	/**
	 * 查询分页数据
	 * @param carInvoice 查询条件
	 * @param carInvoice.page 分页对象
	 * @return
	 */
	@Override
	public Page<CarInvoice> findPage(CarInvoice carInvoice) {
		return super.findPage(carInvoice);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param carInvoice
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(CarInvoice carInvoice) {
	    if (carInvoice.getIsNewRecord()){
            carInvoice.setCarInvoiceCode(codeRuleService.getCode("carInvoice"));
        }
		super.save(carInvoice);
        if (carInvoice.getIsNewRecord()){
            codeRuleService.updateCurrentNumber("carInvoice");
        }
		// 回写客户汽车供需合同
        CustomerCarContract customerCarContract = customerCarContractService.get(carInvoice.getCustomerCarCon().getCustomerCarContractId());
        List<CustomerCarContractDetail> customerCarContractDetailList = customerCarContract.getCustomerCarContractDetailList();
        for (CustomerCarContractDetail customerCarContractDetail : customerCarContractDetailList) {
            if (carInvoice.getFrameNumber().equals(customerCarContractDetail.getFrameNumber())){
                customerCarContractDetail.setPrice(carInvoice.getUntaxAmount());
                customerCarContractDetail.setAmount(carInvoice.getTaxAmount());
                customerCarContractDetail.setInvoiceCode(carInvoice.getCarInvoiceCode());
                customerCarContractDetail.setInvoiceDate(carInvoice.getDate());
                customerCarContractDetail.setInvoiceId(carInvoice.getCarInvoiceId());
            }
        }
        customerCarContractService.save(customerCarContract);

    }
	
	/**
	 * 更新状态
	 * @param carInvoice
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(CarInvoice carInvoice) {
		super.updateStatus(carInvoice);
	}
	
	/**
	 * 删除数据
	 * @param carInvoice
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(CarInvoice carInvoice) {
		super.delete(carInvoice);
	}
	
}